<?php


namespace App\Models\CacheModel\zl1_relation;


use App\Models\Api\BaseCache;
use think\Cache;
use think\Log;

class Zl1RelationAnalysisIdeaSubmitCase extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl1_relation_analysis_idea_submit_case';

    /**
     * 保存方案信息
     */
    public function saveCaseInfo(array $caseData)
    {
        // 入库方案信息
        $this->save($caseData);
        // 缓存方案信息
        Cache::set('relation_analysis:submit_case:' . $caseData['uid'], [
            'rids' => $caseData['rids'],
            'tag_info' => $caseData['tag_info'],
        ], 3600);
    }

    /**
     * 获取方案信息
     * @param int $uid
     * @return array|mixed|null
     */
    public function getCaseInfo(int $uid)
    {
        $caseInfo = Cache::get('relation_analysis:submit_case:' . $uid, null);

        if ($caseInfo)
        {
            return $caseInfo;
        }
        try
        {
            $time = time() - 3600;
            $caseInfo = $this->where(['uid' => $uid, 'create_time' => ['>', $time]])->order('id desc')->field('rids,tag_info')->findOrFail()->toArray();

            return $caseInfo;
        }
        catch (\Exception $e)
        {
            Log::write('Zl1RelationAnalysisIdeaSubmitCase model => getCaseInfo()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
            return null;
        }
    }
}
